package com.quantdo.qtm.mysql.service.monitor.impl.rule;

import java.util.Objects;

import org.springframework.stereotype.Service;

import com.quantdo.qtm.mysql.pojo.model.MysqlStatusCurrDo;
import com.quantdo.qtm.mysql.pojo.model.MysqlStatusPointDo;
import com.quantdo.qtm.mysql.service.monitor.SingleUpdateOpareService;

@Service
public class TpsMonitorService implements SingleUpdateOpareService {

    @Override
    public void updateOpare(MysqlStatusCurrDo statusCurrNewDo, MysqlStatusCurrDo statusCurrOldDo, MysqlStatusPointDo pointDo) {
        if (Objects.isNull(statusCurrOldDo) || Objects.isNull(statusCurrOldDo.getComCommit()) || Objects.isNull(statusCurrOldDo.getComRollback())) {
            pointDo.setTps(0);
        } else {
            pointDo.setTps(((statusCurrNewDo.getComCommit() - statusCurrOldDo.getComCommit()) + (statusCurrNewDo.getComRollback() - statusCurrOldDo.getComRollback()))
                    / statusCurrNewDo.getIntervalSecond());
        }
    }

}
